import 'package:flutter/material.dart';

class KqOldSectionGridView<E> extends StatefulWidget {
  ///分区数
  final int section;

  ///对应section分区内的内容数组
  final List<E> Function(int section) itemList;

  ///设置每个分区的gridView样式
  final SliverGridDelegateWithFixedCrossAxisCount Function(int section)
      axisCount;

  ///列表头部UI
  final Widget Function()? gridHeaderBuilder;

  ///列表尾部UI
  final Widget Function()? gridFooterBuilder;

  ///分组头部UI
  final Widget Function(int section)? sectionHeaderBuilder;

  ///分组尾部UI
  final Widget Function(int section)? sectionFooterBuilder;

  ///cell的对应UI
  final Widget Function(int section, int row, E object) itemBuilder;

  const KqOldSectionGridView({
    super.key,
    required this.section,
    required this.itemList,
    required this.axisCount,
    this.gridHeaderBuilder,
    this.gridFooterBuilder,
    this.sectionHeaderBuilder,
    this.sectionFooterBuilder,
    required this.itemBuilder,
  });

  @override
  State<KqOldSectionGridView> createState() => _KqOldSectionGridViewState<E>();
}

class _KqOldSectionGridViewState<E> extends State<KqOldSectionGridView<E>> {
  List<Widget> slivers = [];
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _buildBody();
  }

  Widget _buildBody() {
    return CustomScrollView(
      slivers: getSlivers(),
    );
  }

  ///listView头部区域
  Widget _tableViewHeaderView(Widget child) {
    return SliverToBoxAdapter(
      child: child,
    );
  }

  ///listView头部区域
  Widget _tableViewFooterView(Widget child) {
    return SliverToBoxAdapter(
      child: child,
    );
  }

  ///Section头部区域布局
  Widget _buildSectionHeader({required int section, required Widget child}) {
    //后期处理section
    return SliverToBoxAdapter(
      child: child,
    );
  }

  ///Section尾部区域布局
  Widget _buildSectionFooter({required int section, required Widget child}) {
    //后期处理section
    return SliverToBoxAdapter(
      child: child,
    );
  }

  ///分区样式
  Widget _buildSliverList({required int section, required List<E> list}) {
    return SliverGrid(
        delegate: SliverChildBuilderDelegate(
            ((context, index) =>
                widget.itemBuilder(section, index, list[index])),
            childCount: list.length),
        gridDelegate: widget.axisCount(section));
  }

  List<Widget> getSlivers() {
    slivers = [];
    if (widget.gridHeaderBuilder != null) {
      slivers.add(_tableViewHeaderView(widget.gridHeaderBuilder!()));
    }
    for (int i = 0; i < widget.section; i++) {
      List<E> rowList = widget.itemList(i);
      if (widget.sectionHeaderBuilder != null) {
        slivers.add(_buildSectionHeader(
          section: i,
          child: widget.sectionHeaderBuilder!(i),
        ));
      }
      slivers.add(
          _buildSliverList(section: i, list: rowList.whereType<E>().toList()));
      if (widget.sectionFooterBuilder != null) {
        slivers.add(_buildSectionFooter(
          section: i,
          child: widget.sectionFooterBuilder!(i),
        ));
      }
    }
    if (widget.gridFooterBuilder != null) {
      slivers.add(_tableViewFooterView(widget.gridFooterBuilder!()));
    }
    return slivers;
  }
}
